package com.spider.silence.output;

import com.spider.silence.itf.output.Writer;
import com.spider.silence.utils.FtpUtil;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.log4j.Logger;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Map;
import java.util.Set;

/**
 * Created by 夏茂轩 on 2017/1/10.
 */
public class FTPWriter implements Writer {
    Logger logger =Logger.getLogger(FTPWriter.class);
    FTPClient ftpClient =FtpUtil.getClient();
    @Override
    @Deprecated
    public boolean  write(String path,String fileName,String content) throws Exception {
        OutputStreamWriter out=null;
        BufferedWriter pw=null;
        try {
            ftpClient.sendNoOp();
            path = path + "/";
            boolean flag=createDirecroty(path,ftpClient);
            if(flag){
                ftpClient.changeWorkingDirectory(path);//如果没有文件夹就创建文件夹
                ftpClient.deleteFile(fileName);
                out = new OutputStreamWriter(ftpClient.appendFileStream(fileName));
                pw = new BufferedWriter(out);
                pw.write(content);
                out.flush();
                pw.flush();
                pw.close();
                out.close();
                while (!ftpClient.completePendingCommand()){};
                ftpClient.changeWorkingDirectory("/");
            }else{
                return false;
            }
            Thread.sleep(2000);
            return  true;
        } catch (Exception e) {
            logger.error("["+path+"/"+fileName+"]"+"----写入错误",e);
            e.printStackTrace();
            return  false;
        }
    }

    @Override
    public boolean write(String path, String fileName, String content, Map<String, File> files) throws Exception {
        return false;
    }

    public boolean write(String path, String fileName, String content, Set<File> files) throws Exception {
        return false;
    }

    /** */
    /**
     * 递归创建远程服务器目录
     *
     * @param remote
     *            远程服务器文件绝对路径
     *
     * @return 目录创建是否成功
     * @throws IOException
     */
    public boolean createDirecroty(String remote,FTPClient ftp) throws IOException {
        boolean success = true;
        String directory = remote.substring(0, remote.lastIndexOf("/") + 1);
        // 如果远程目录不存在，则递归创建远程服务器目录
        if (!directory.equalsIgnoreCase("/")
                && !ftp.changeWorkingDirectory(new String(directory))) {
            int start = 0;
            int end = 0;
            if (directory.startsWith("/")) {
                start = 1;
            } else {
                start = 0;
            }
            end = directory.indexOf("/", start);
            while (true) {
                String subDirectory = new String(remote.substring(start, end));
                if (!ftp.changeWorkingDirectory(subDirectory)) {
                    if (ftp.makeDirectory(subDirectory)) {
                        ftp.changeWorkingDirectory(subDirectory);
                    } else {
                        System.out.println("创建目录失败");
                        success = false;
                        return success;
                    }
                }
                start = end + 1;
                end = directory.indexOf("/", start);
                // 检查所有目录是否创建完毕
                if (end <= start) {
                    break;
                }
            }
        }
        return success;
    }
}
